package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.testapplication.R;
import com.example.testapplication.bean.Chapter;
import com.example.testapplication.bean.ChapterItem;

import java.util.List;

public class ChapterAdapter extends BaseExpandableListAdapter {

    private List<Chapter> mDatas;
    private LayoutInflater mInflater;

    private Context mContext;
    public ChapterAdapter(Context context,List<Chapter> datas){
        mContext = context;
        mDatas = datas;
        mInflater = LayoutInflater.from(context);
    }

    // 分组的数量
    @Override
    public int getGroupCount() {
        return mDatas.size();
    }
    // 子分组的位置
    @Override
    public int getChildrenCount(int i) {
        return mDatas.get(i).getChildren().size();
    }

    @Override
    public Object getGroup(int i) {
        return mDatas.get(i);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mDatas.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
    // 重点
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ParentViewHolder parentViewHolder = null;
        if(view == null){
            view = mInflater.inflate(R.layout.item_parent_chapter,viewGroup,false);
            parentViewHolder = new ParentViewHolder();
            parentViewHolder.mTvName = view.findViewById(R.id.id_tv_name);
            view.setTag(parentViewHolder);
        }else{
            parentViewHolder = (ParentViewHolder) view.getTag();
        }
        Chapter chapter = mDatas.get(i);
        parentViewHolder.mTvName.setText(chapter.getName());
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder childViewHolder = null;
        if(view == null){
            view = mInflater.inflate(R.layout.item_child_chapter,viewGroup,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.mTvName = view.findViewById(R.id.id_tv_name);
            view.setTag(childViewHolder);
        }else{
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        ChapterItem chapterItem = mDatas.get(i).getChildren().get(i);
        childViewHolder.mTvName.setText(chapterItem.getName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public static class ParentViewHolder{
        TextView mTvName;
    }

    public static class ChildViewHolder{
        TextView mTvName;
    }
}
